﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

namespace ProjectEulerSolutions.Problems
{
    /*
     * If a box contains twenty-one coloured discs, composed of fifteen blue discs and six red discs, and two discs were taken at random, it can be seen that the probability of taking two blue discs, P(BB) = (15/21)×(14/20) = 1/2.

The next such arrangement, for which there is exactly 50% chance of taking two blue discs at random, is a box containing eighty-five blue discs and thirty-five red discs.

By finding the first arrangement to contain over 1012 = 1,000,000,000,000 discs in total, determine the number of blue discs that the box would contain.

     * */
    class Problem100 : IProblem
    {
        public string Calculate()
        {
            //jednom kad skontas da je slijedeci nazivnik veci od trenutni * (trenutni / prosli),
            //onda je lako opalit estimate na slijedeci nazivnik
            //sto se tice brojnika, on je prvi broj veci od nazivnik / sqrt(2)
            //nula milisekundi - rofl

            double root2 = Math.Sqrt(2);

            long previousi = 1;

            for (long i = 1; i < 2000000000000; i++)
            {
                long numerator = (long)(i / root2) + 1;

                Fraction fraction1 = new Fraction(numerator, i);
                Fraction fraction2 = new Fraction(numerator - 1, i - 1);

                if (fraction1.Numerator == fraction2.Denominator)
                {
                    if (fraction1.Denominator == 2 * fraction2.Numerator)
                    {
                        Console.WriteLine("{0}/{1}", numerator, i);
                        if (i > 1000000000000)
                            return numerator.ToString();
                        long next = (long)(i * (i / (double)previousi));
                        previousi = i;
                        i = next;
                    }
                }
                else if (fraction1.Numerator * 2 == fraction2.Denominator)
                {
                    if (fraction1.Denominator == fraction2.Numerator)
                    {
                        Console.WriteLine("{0}/{1}", numerator, i);
                        if (i > 1000000000000)
                            return numerator.ToString();
                        long next = (long)(i * (i / (double)previousi));
                        previousi = i;
                        i = next;
                    }
                }
            }

            return "Izvan intervala...";
        }
    }
}
